package com.cincc.cinccimdemo.ui.me.favorite;

import android.widget.Toast;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.viewmodel.FavoriteViewModel;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.CollectModel;

public class FavoriteListActivity extends IMBaseActivity {
    private static final String TAG = "FavoriteListActivity";

    @Override
    protected int contentLayout() {
        return R.layout.activity_fav_list;
    }

    RecyclerView recyclerView;

    @Override
    protected void bindViews() {
        super.bindViews();
        recyclerView = findViewById(R.id.recyclerView);
    }

    FavoriteViewModel favoriteViewModel;
    FavoriteListAdapter favoriteListAdapter;

    @Override
    protected void afterViews() {
        super.afterViews();
        favoriteListAdapter = new FavoriteListAdapter(this);
        recyclerView.setAdapter(favoriteListAdapter);
        favoriteViewModel = new ViewModelProvider(this).get(FavoriteViewModel.class);
        favoriteViewModel.favoriteListLiveData().observe(this, groups -> {
            favoriteListAdapter.setFavoriteList(groups);
        });
    }

    public void delFav(CollectModel item) {
        IMSipPhoneCtrl.getInstance().delCollect(item.msgId, imResponse -> {
            String result = imResponse.getCode() == 0 ? "删除成功" : imResponse.getMsg();
            runOnUiThread(() -> {
                Toast.makeText(FavoriteListActivity.this, result, Toast.LENGTH_SHORT).show();
                favoriteListAdapter.removeFavoriteItem(item.msgId);
            });
        });
    }
}
